package com.tangcan.blog.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tangcan.blog.Entity.BlogImg;
import com.tangcan.blog.Mapper.BlogImgMapper;
import com.tangcan.blog.Utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Time;
import java.util.Base64;
import java.util.Date;
import java.util.Map;

@Service
@EnableAutoConfiguration
public class ImgService {
    @Autowired
    BlogImgMapper blogImgMapper;

    TokenUtils tokenUtils;


    public void GetImgById(String id, HttpServletResponse response)
    {

        response.setHeader("Content-Type","image/*");
        //解码base64图片
        Base64.Decoder decoder = Base64.getDecoder();
        String base64code = blogImgMapper.GetImgById(id);
        int index = base64code.indexOf("base64");
        String result=base64code.substring(index+7);
        byte[] decodebyte = decoder.decode(result);
        try {
            OutputStream res = response.getOutputStream();
            res.write(decodebyte);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    //上传图片
    public JSONObject InsetImg(String data)
    {
        JSONObject json = JSON.parseObject(data);
        String token = json.getString("token");
        JSONObject resJson = new JSONObject();
        if (tokenUtils.verify(token))
        {
            Map tokenInfo = tokenUtils.getTokenInfo(token);
            String authority = tokenInfo.get("authority").toString().replace("\"","");
            if (authority.equals("admin"))
            {
                int UserId = Integer.parseInt(tokenInfo.get("id").toString().replace("\"",""));
                long time = new Date().getTime();
                String BlogImgId = String.format("blog_%d_time_%d",UserId,time);
                String ImgData = json.getString("imgData");
                BlogImg blogImg = new BlogImg(BlogImgId,ImgData,UserId);
                int recall = blogImgMapper.InsertImg(blogImg);
                if (recall > 0)
                {
                    resJson.put("code","ok");
                    resJson.put("id",BlogImgId);
                    resJson.put("info","上传图片成功");
                    return resJson;
                }
                else
                {
                    resJson.put("code","err");
                    resJson.put("info","上传失败");
                    return resJson;
                }
            }
            else {
                resJson.put("code","err");
                resJson.put("info","不是管理员");
                return resJson;
            }
        }
        else
        {
            resJson.put("code","err");
            resJson.put("info","token过期");
            return resJson;
        }



    }
}
